const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function time(time) {
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;
  var n = timestamp * 1000;
  var date = new Date(n);
  var timemonth = parseInt(time[5] + time[6])
  var month = date.getMonth() + 1
  let timeday = parseInt(time[8] + time[9])
  let day = date.getDate()
  var dayfinal = ''
  if ((day - timeday) == 0) {
    dayfinal = '今天'
  } else if ((day - timeday) == 1) {
    dayfinal = '昨天'
  } else {
    dayfinal = timemonth + "-" + timeday
  }
  return dayfinal +" "+ time[11] + time[12] + time[13] + time[14] + time[15]
}

module.exports = {
  formatTime: formatTime,
  time: time
}